package com.engineering.elink.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.engineering.elink.co.VideoCommentCo;
import com.engineering.elink.domain.VideoComment;
import com.engineering.elink.domain.VideoPraise;
import com.engineering.elink.service.VideoCommentService;
import com.engineering.elink.videodao.ChapterMapper;
import com.engineering.elink.videodao.CourseMapper;
import com.engineering.elink.videodao.VideoCommentMapper;
import com.engineering.elink.videodao.VideoMapper;
import com.engineering.elink.videodao.VideoPraiseMapper;
import com.zxy.core.Pagination;
import com.zxy.core.co.BaseCo;
import com.zxy.core.enums.ErrorType;
import com.zxy.core.util.CollectionUtil;
import com.zxy.core.util.ResJsonUtil;

/**
 * 
 * 视频评论的业务实现类
 * 
 * ClassName: VideoCommentServiceImpl <br/> 
 * Function: ADD FUNCTION. <br/> 
 * Reason: ADD REASON(可选). <br/> 
 * date: 2016年10月20日 下午4:18:22 <br/> 
 * 
 * @author zengxinyan 
 * @version  
 * @since JDK 1.8
 */
@Service
public class VideoCommentServiceImpl implements VideoCommentService {
	
	@Autowired
	private VideoCommentMapper videoCommentMapper;
	
	@Autowired
	private VideoPraiseMapper videoPraiseMapper;
	
	@Autowired
	private VideoMapper videoMapper;
	
	@Autowired
	private ChapterMapper chapterMapper;
	
	@Autowired
	private CourseMapper courseMapper;
	
	/**
	 * 
	 * 简单描述该方法的实现功能（可选）. 
	 * @see com.engineering.elink.service.VideoCommentService#getTopCommentsByVideoId(com.engineering.elink.co.VideoCommentCo)
	 */
	@Override
	public Map<String, Object> getTopCommentsByVideoId(VideoCommentCo co) {
		Map<String , Object>map= new HashMap<String, Object>();
		
		map.put("list",videoCommentMapper.selectTopCommentsByVideoId(co));
		map.put("Pagination", new Pagination(co, videoCommentMapper.selectTopCommentsCount(co)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
	}
	
	/**
	 * 
	 * 简单描述该方法的实现功能（可选）. 
	 * @see com.engineering.elink.service.VideoCommentService#getAllCommentsByTopId(com.engineering.elink.co.BaseCo, int)
	 */
	@Override
	public Map<String, Object> getAllCommentsByTopId(BaseCo co, int topId,boolean parentFlag) {
		Map<String , Object> map= new HashMap<String, Object>();
		
		map.put("list",videoCommentMapper.selectAllCommentsByTopId(co,topId,parentFlag));
		map.put("Pagination", new Pagination(co, videoCommentMapper.selectCount(topId,parentFlag)));
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,map);
	}
	

	

	


	/**
	 * 
	 * 简单描述该方法的实现功能（可选）. 
	 * @see com.engineering.elink.service.VideoCommentService#addPraise(java.lang.Integer, java.lang.Integer, java.lang.String)
	 */
	@Override
	public Map<String, Object> addPraise(Integer evalCommentId,Integer praiseType,String userId) {
		//1视频
		int count = videoPraiseMapper.select(evalCommentId,praiseType,userId,1);
		if(count == 0){
			VideoComment c  = videoCommentMapper.selectByPrimaryKey(evalCommentId);
			if(c != null){
				videoPraiseMapper.insertSelective(new VideoPraise(evalCommentId,praiseType,userId,1));
			
				updateComment(praiseType,c);
				
				//更新上级评论的顶/踩数量
				List<VideoComment> eList=videoCommentMapper.selectAllCommentByRootId(c.getCommentId());
				if(CollectionUtil.collectionIsNotEmpty(eList)){
					for(VideoComment e:eList){
						updateComment(praiseType,e);
					}
				}
				
				return ResJsonUtil.getMap(ErrorType.SUCCESS,c);
			}else{
				return ResJsonUtil.getMap(ErrorType.COMMENT_PRAISE_ERROR);
			}	
		}else {
			return ResJsonUtil.getMap(ErrorType.PRAISE_ERROR);
		}
		
	}
	
	private void updateComment(Integer praiseType,VideoComment c){
		//更新评价的顶/踩数量
		if(praiseType.intValue()==1){//顶
			c.setPraiseCount(c.getPraiseCount()+1);
		}else{//踩
			c.setStampCount(c.getStampCount()+1);
		}
		videoCommentMapper.updateByPrimaryKeySelective(c);
	}

	/**
	 * 
	 * 简单描述该方法的实现功能（可选）. 
	 * @see com.engineering.elink.service.VideoCommentService#getCommentForMe(com.engineering.elink.co.BaseCo, java.lang.String)
	 */
	@Override
	public Map<String, Object> getCommentForMe(BaseCo co,String userId) {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,videoMapper.getCommentForMe(co ,userId));
	}

	/**
	 * 
	 * 简单描述该方法的实现功能（可选）. 
	 * @see com.engineering.elink.service.VideoCommentService#getMyComment(com.engineering.elink.co.BaseCo, java.lang.String)
	 */
	@Override
	public Map<String, Object> getMyComment(BaseCo co,String userId) {
		return ResJsonUtil.getMap(ErrorType.SUCCESS,videoMapper.getMyComment(co ,userId));
	}
}
